﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GTP.AppFrame.PrototypeTools.Business;
using GTP.AppFrame.PrototypeTools.Common;

namespace GTP.AppFrame.PrototypeTools
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            InitControls();
        }
        /// <summary>
        /// 初始化一些控件的值
        /// </summary>
        private void InitControls()
        {
            txtParentName.Text = "Simple";
            txtParentPackage.Text = "BasicBill";
            txtParentTable.Text = "Y_SIMPLE";
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                //数据校验
                if (string.IsNullOrEmpty(txtName.Text))
                {
                    MessageBox.Show("名称不能为空");
                    return;
                }
                if (string.IsNullOrEmpty(txtDisplayName.Text))
                {
                    MessageBox.Show("显示名称不能为空");
                    return;
                }
                if (string.IsNullOrEmpty(txtNamespace.Text))
                {
                    MessageBox.Show("命名空间不能为空");
                    return;
                }
                List<EntityProperty> mainPropertyList = GetMainProperty();
                BizProperty propertyInfo = new BizProperty();
                propertyInfo.Name = txtName.Text;
                propertyInfo.DisplayName = txtDisplayName.Text;
                propertyInfo.BizNameSpace = txtNamespace.Text;
                propertyInfo.BizPackageName = txtPakageName.Text;
                propertyInfo.MainEntityPropetyList = mainPropertyList;
                propertyInfo.ParentName = txtParentName.Text;
                propertyInfo.ParentPackage = txtParentPackage.Text;
                propertyInfo.ParentTable = txtParentTable.Text;
                BizForm bizForm = new BizForm(propertyInfo);
                bizForm.Generate();
                MessageBox.Show("生成成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("表单生成失败：" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Log.AddErrorLog("生成表单", ex.Message);
            }
        }
        private void btnFilterProperty_Click(object sender, EventArgs e)
        {
            try
            {
                txtPropertyList.Text = FindUtility.FindLabel(txtOutput.Text);
                MessageBox.Show("获取成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取失败：" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Log.AddErrorLog("获取属性", ex.Message);
            }
        }

        private void btnGetSub_Click(object sender, EventArgs e)
        {
            try
            {
                //子表的临时解决方案，只输出GridView的列
                List<EntityProperty> mainPropertyList = GetMainProperty();
                Business.Page.GTPListPage listPage = new Business.Page.GTPListPage();
                txtOutput.Text = listPage.GenneratorGridColumn(mainPropertyList);
                MessageBox.Show("生成成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("生成失败：" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Log.AddErrorLog("生成子表", ex.Message);
            }
        }
        /// <summary>
        /// 取主实体的属性
        /// </summary>
        /// <returns></returns>
        private List<EntityProperty> GetMainProperty()
        {
            List<EntityProperty> propertyList = new List<EntityProperty>();
            if (!string.IsNullOrEmpty(txtPropertyList.Text))
            {
                string property = string.Empty;
                int counter = 0;
                using (StringReader reader = new StringReader(txtPropertyList.Text))
                {
                    do
                    {
                        property = reader.ReadLine();
                        counter++;
                        if (!string.IsNullOrWhiteSpace(property))
                        {
                            property = property.Trim();
                            EntityProperty entityProperty = new EntityProperty();
                            entityProperty.ID = counter;
                            //属性末尾为[T-D]为日期类型，[T-L]参照类型，默认为文本
                            if (property.Contains("[T-D]") && property.LastIndexOf("[T-D]") == property.Length - 5)
                            {
                                property = property.Replace("[T-D]", "");
                                entityProperty.Type = EntityProperyType.Date;
                            }
                            else if (property.Contains("[T-L]") && property.LastIndexOf("[T-L]") == property.Length - 5)
                            {
                                property = property.Replace("[T-L]", "");
                                entityProperty.Type = EntityProperyType.Lookup;
                            }
                            else
                            {
                                entityProperty.Type = EntityProperyType.Text;
                            }
                            entityProperty.Name = property;
                            propertyList.Add(entityProperty);
                        }
                    }
                    while (!string.IsNullOrEmpty(property));
                }

            }
            return propertyList;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtPropertyList.Text = string.Empty;
            txtOutput.Text = string.Empty;
        }

    }
}
